﻿Imports System.ServiceModel
Imports System.ServiceModel.Activation

<ServiceContract(Namespace:="")>
<SilverLightFaultBehavior>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class WebServices
    <OperationContract()>
    Public Function UserAccountCreate(pvkey As String, user_civilite As String, user_prenom As String, user_nom As String, user_email As String, user_password As String) As Long
        '0 : no init
        '-1 : account with this email exist
        '-2 : error processing creation
        'else : account ok with ID returning
        Dim ReturnData As Long = 0
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim UserExist = From user In dbcon.users
                                Where user.user_email = user_email
                                Select user

                If UserExist.Count > 0 Then
                    ReturnData = -1
                Else
                    Dim NewUser As New users
                    NewUser.user_civilite = user_civilite
                    NewUser.user_prenom = user_prenom
                    NewUser.user_nom = user_nom
                    NewUser.user_email = user_email
                    NewUser.user_password = user_password
                    dbcon.AddTousers(NewUser)
                    dbcon.SaveChanges()
                    Dim UserSearch = From user In dbcon.users
                                     Where user.user_email = user_email And user.user_password = user_password
                                     Select user

                    If UserSearch.Count > 0 Then
                        ReturnData = UserSearch.First.id_user
                    End If
                End If
            Catch ex As Exception
                ReturnData = -2
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function UserAccountEdit(pvkey As String, id_user As Long, user_civilite As String, user_prenom As String, user_nom As String, user_email As String, user_password As String) As Boolean
        Dim ReturnData As Boolean = False
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim UserExist = From user In dbcon.users
                                Where user.id_user = id_user
                                Select user

                If UserExist.Count = 1 Then
                    UserExist.First.user_civilite = user_civilite
                    UserExist.First.user_prenom = user_prenom
                    UserExist.First.user_nom = user_nom
                    UserExist.First.user_email = user_email
                    UserExist.First.user_password = user_password
                    dbcon.SaveChanges()
                    ReturnData = True
                End If
            Catch ex As Exception
                ReturnData = False
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function UserAccountLogging(pvkey As String, user_email As String, user_password As String) As Long
        '0 : no init
        '-1 : no account with this email exist
        '-2 : error processing creation
        'else : account ok with ID returning
        Dim ReturnData As Long = 0
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim UserExist = From user In dbcon.users
                                Where user.user_email = user_email And user.user_password = user_password
                                Select user

                If UserExist.Count = 1 Then
                    ReturnData = UserExist.First.id_user
                Else
                    ReturnData = -1
                End If
            Catch ex As Exception
                ReturnData = -2
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function UserAccountDetail(pvkey As String, id_user As Long) As users
        Dim ReturnData As users = Nothing
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim UserExist = From user In dbcon.users
                                Where user.id_user = id_user
                                Select user

                If UserExist.Count = 1 Then
                    ReturnData = UserExist.First
                Else
                    ReturnData = Nothing
                End If
            Catch ex As Exception
                ReturnData = Nothing
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function UserAccountDelete(pvkey As String, id_user As Long) As Boolean
        Dim ReturnData As Boolean = False
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim UserExist = From user In dbcon.users
                                Where user.id_user = id_user
                                Select user

                If UserExist.Count = 1 Then
                    dbcon.DeleteObject(UserExist.First)
                    dbcon.SaveChanges()
                    ReturnData = True
                Else
                    ReturnData = False
                End If
            Catch ex As Exception
                ReturnData = False
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function UserAccountList(pvkey As String) As List(Of users)
        Dim ReturnData As List(Of users) = Nothing
        If pvkey = My.Settings.pvkey Then
            Try

                Dim dbcon As New ISIS_AssoEntities
                Dim UserExist = From user In dbcon.users
                                Select user

                ReturnData = UserExist.ToList

            Catch ex As Exception
                ReturnData = Nothing
            End Try
        End If
        Return ReturnData
    End Function


    <OperationContract()>
    Public Function AssoCreate(pvkey As String, nom_asso As String, adresse_l1 As String, adresse_l2 As String, adresse_l3 As String, adresse_cp As String, adresse_ville As String, telephone As String, telecopie As String, email As String, id_activite As Integer) As Integer
        '0 : no init
        '-1 : account with this email exist
        '-2 : error processing creation
        'else : account ok with ID returning
        Dim ReturnData As Integer = 0
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim AssoExist = From Asso In dbcon.associations
                                Where Asso.nom_asso = nom_asso And Asso.adresse_ville = adresse_ville
                                Select Asso

                If AssoExist.Count > 0 Then
                    ReturnData = -1
                Else
                    Dim NewAsso As New associations
                    NewAsso.nom_asso = nom_asso
                    NewAsso.adresse_l1 = adresse_l1
                    NewAsso.adresse_l2 = adresse_l2
                    NewAsso.adresse_l3 = adresse_l3
                    NewAsso.adresse_cp = adresse_cp
                    NewAsso.adresse_ville = adresse_ville
                    NewAsso.telephone = telephone
                    NewAsso.telecopie = telecopie
                    NewAsso.email = email
                    NewAsso.id_activite = id_activite
                    dbcon.AddToassociations(NewAsso)
                    dbcon.SaveChanges()
                    Dim AssoSearch = From Asso In dbcon.associations
                                     Where Asso.nom_asso = nom_asso And Asso.adresse_ville = adresse_ville
                                     Select Asso

                    If AssoSearch.Count > 0 Then
                        ReturnData = AssoSearch.First.id_asso
                    End If
                End If
            Catch ex As Exception
                ReturnData = -2
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function AssoEdit(pvkey As String, id_asso As Integer, nom_asso As String, adresse_l1 As String, adresse_l2 As String, adresse_l3 As String, adresse_cp As String, adresse_ville As String, telephone As String, telecopie As String, email As String, id_activite As Integer) As Boolean
        Dim ReturnData As Boolean = False
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim AssoExist = From Asso In dbcon.associations
                                Where Asso.id_asso = id_asso
                                Select Asso
                If AssoExist.Count = 1 Then
                    AssoExist.First.nom_asso = nom_asso
                    AssoExist.First.adresse_l1 = adresse_l1
                    AssoExist.First.adresse_l2 = adresse_l2
                    AssoExist.First.adresse_l3 = adresse_l3
                    AssoExist.First.adresse_cp = adresse_cp
                    AssoExist.First.adresse_ville = adresse_ville
                    AssoExist.First.telephone = telephone
                    AssoExist.First.telecopie = telecopie
                    AssoExist.First.email = email
                    AssoExist.First.id_activite = id_activite
                    dbcon.SaveChanges()
                End If
            Catch ex As Exception
                ReturnData = False
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function AssoDetail(pvkey As String, id_asso As Integer) As associations
        Dim ReturnData As associations = Nothing
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim AssoExist = From Asso In dbcon.associations
                                Where Asso.id_asso = id_asso
                                Select Asso

                If AssoExist.Count = 1 Then
                    ReturnData = AssoExist.First
                Else
                    ReturnData = Nothing
                End If
            Catch ex As Exception
                ReturnData = Nothing
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function AssoDelete(pvkey As String, id_asso As Integer) As Boolean
        Dim ReturnData As Boolean = False
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim AssoExist = From Asso In dbcon.associations
                                Where Asso.id_asso = id_asso
                                Select Asso

                If AssoExist.Count = 1 Then
                    dbcon.DeleteObject(AssoExist.First)
                    dbcon.SaveChanges()
                    ReturnData = True
                Else
                    ReturnData = False
                End If
            Catch ex As Exception
                ReturnData = False
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function AssoList(pvkey As String) As List(Of associations)
        Dim ReturnData As List(Of associations) = Nothing
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim AssoExist = From asso In dbcon.associations
                                Select asso

                ReturnData = AssoExist.ToList
            Catch ex As Exception
                ReturnData = Nothing
            End Try
        End If
        Return ReturnData
    End Function

    <OperationContract()>
    Public Function AssoListOfUsers(pvkey As String, id_user As Long) As List(Of associations)
        Dim ReturnData As List(Of associations) = Nothing
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim AssoExist = From asso In dbcon.associations
                                Join user_right In dbcon.users_rights On user_right.id_asso Equals asso.id_asso
                                Where user_right.id_user = id_user
                                Select asso

                ReturnData = AssoExist.ToList
            Catch ex As Exception
                ReturnData = Nothing
            End Try
        End If
        Return ReturnData
    End Function



    <OperationContract()>
    Public Function UserRightCreate(pvkey As String, id_asso As Integer, id_user As Long, rights_full As Boolean, rights_members As Boolean, rights_communication As Boolean, rights_events As Boolean) As Long
        '0 : no init
        '-1 : UserRightExist exist
        '-2 : error processing creation
        'else : UserRight ok with ID returning
        Dim ReturnData As Integer = 0
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim UserRightExist = From ur In dbcon.users_rights
                                Where ur.id_asso = id_asso And ur.id_user = id_user
                                Select ur

                If UserRightExist.Count > 0 Then
                    ReturnData = -1
                Else
                    Dim NewUserRight As New users_rights
                    NewUserRight.id_asso = id_asso
                    NewUserRight.id_user = id_user
                    NewUserRight.rights_full = rights_full
                    NewUserRight.rights_members = rights_members
                    NewUserRight.rights_communication = rights_communication
                    NewUserRight.rights_events = rights_events
                    dbcon.AddTousers_rights(NewUserRight)
                    dbcon.SaveChanges()
                    Dim UserRightSearch = From ur In dbcon.users_rights
                                     Where ur.id_asso = id_asso And ur.id_user = id_user
                                     Select ur

                    If UserRightSearch.Count > 0 Then
                        ReturnData = UserRightSearch.First.id_right
                    End If
                End If
            Catch ex As Exception
                ReturnData = -2
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function UserRightEdit(pvkey As String, id_right As Long, id_asso As Integer, id_user As Long, rights_full As Boolean, rights_members As Boolean, rights_communication As Boolean, rights_events As Boolean) As Boolean
        Dim ReturnData As Boolean = False
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim UserRightExist = From ur In dbcon.users_rights
                                Where ur.id_right = id_right
                                Select ur
                If UserRightExist.Count = 1 Then
                    UserRightExist.First.id_asso = id_asso
                    UserRightExist.First.id_user = id_user
                    UserRightExist.First.rights_full = rights_full
                    UserRightExist.First.rights_members = rights_members
                    UserRightExist.First.rights_communication = rights_communication
                    UserRightExist.First.rights_events = rights_events

                    dbcon.SaveChanges()
                End If
            Catch ex As Exception
                ReturnData = False
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function UserRightDetail(pvkey As String, id_right As Integer) As users_rights
        Dim ReturnData As users_rights = Nothing
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim UserRightExist = From ur In dbcon.users_rights
                                Where ur.id_right = id_right
                                Select ur

                If UserRightExist.Count = 1 Then
                    ReturnData = UserRightExist.First
                Else
                    ReturnData = Nothing
                End If
            Catch ex As Exception
                ReturnData = Nothing
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function UserRightDelete(pvkey As String, id_right As Integer) As Boolean
        Dim ReturnData As Boolean = False
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim UserRightExist = From ur In dbcon.users_rights
                                Where ur.id_right = id_right
                                Select ur

                If UserRightExist.Count = 1 Then
                    dbcon.DeleteObject(UserRightExist.First)
                    dbcon.SaveChanges()
                    ReturnData = True
                Else
                    ReturnData = False
                End If
            Catch ex As Exception
                ReturnData = False
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function UserRightList(pvkey As String) As List(Of users_rights)
        Dim ReturnData As List(Of users_rights) = Nothing
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim UserRightExist = From ur In dbcon.users_rights
                                Select ur

                ReturnData = UserRightExist.ToList
            Catch ex As Exception
                ReturnData = Nothing
            End Try
        End If
        Return ReturnData
    End Function

    <OperationContract()>
    Public Function ActivitesList(pvkey As String) As List(Of activites_associations)
        Dim ReturnData As List(Of activites_associations) = Nothing
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim ActivitesExist = From act In dbcon.activites_associations
                                Select act

                ReturnData = ActivitesExist.ToList
            Catch ex As Exception
                ReturnData = Nothing
            End Try
        End If
        Return ReturnData
    End Function
    <OperationContract()>
    Public Function ActivitesList2(pvkey As String) As List(Of activites_associations)
        Dim ReturnData As List(Of activites_associations) = Nothing
        If pvkey = My.Settings.pvkey Then
            Try
                Dim dbcon As New ISIS_AssoEntities
                Dim ActivitesExist = From act In dbcon.activites_associations
                                Select act

                ReturnData = ActivitesExist.ToList
            Catch ex As Exception
                ReturnData = Nothing
            End Try
        End If
        Return ReturnData
    End Function

End Class
